{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "mnist = tf.keras.datasets.mnist\n",
    "(train_images,train_labels),(test_images,test_labels) = mnist.load_data()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_images = train_images / 255.0\n",
    "test_images = test_images / 255.0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_labels_ohe = tf.one_hot(train_labels,depth=10).numpy()\n",
    "test_labels_ohe = tf.one_hot(test_labels,depth = 10).numpy()\n",
    "\n",
    "myW = tf.Variable(np.zeros((784,64)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Flatten(input_shape = (28,28)))\n",
    "model.add(tf.keras.layers.Dense(units = 64,activation = \"relu\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.Dense(units = 32,kernel_initializer = \"normal\",activation = \"relu\"))\n",
    "model.add(tf.keras.layers.Dense(units = 10,activation = \"softmax\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten (Flatten)            (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 64)                50240     \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 32)                2080      \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 10)                330       \n",
      "=================================================================\n",
      "Total params: 52,650\n",
      "Trainable params: 52,650\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer=\"adam\",loss = \"categorical_crossentropy\",metrics=[\"accuracy\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_epochs = 10\n",
    "batch_size = 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "1600/1600 - 1s - loss: 0.3522 - accuracy: 0.9009 - val_loss: 0.1884 - val_accuracy: 0.9455\n",
      "Epoch 2/10\n",
      "1600/1600 - 1s - loss: 0.1583 - accuracy: 0.9537 - val_loss: 0.1365 - val_accuracy: 0.9606\n",
      "Epoch 3/10\n",
      "1600/1600 - 1s - loss: 0.1135 - accuracy: 0.9658 - val_loss: 0.1154 - val_accuracy: 0.9669\n",
      "Epoch 4/10\n",
      "1600/1600 - 1s - loss: 0.0883 - accuracy: 0.9738 - val_loss: 0.1097 - val_accuracy: 0.9691\n",
      "Epoch 5/10\n",
      "1600/1600 - 1s - loss: 0.0724 - accuracy: 0.9772 - val_loss: 0.1049 - val_accuracy: 0.9693\n",
      "Epoch 6/10\n",
      "1600/1600 - 1s - loss: 0.0592 - accuracy: 0.9812 - val_loss: 0.1086 - val_accuracy: 0.9697\n",
      "Epoch 7/10\n",
      "1600/1600 - 1s - loss: 0.0502 - accuracy: 0.9840 - val_loss: 0.1086 - val_accuracy: 0.9693\n",
      "Epoch 8/10\n",
      "1600/1600 - 1s - loss: 0.0437 - accuracy: 0.9861 - val_loss: 0.1059 - val_accuracy: 0.9702\n",
      "Epoch 9/10\n",
      "1600/1600 - 1s - loss: 0.0367 - accuracy: 0.9883 - val_loss: 0.1087 - val_accuracy: 0.9707\n",
      "Epoch 10/10\n",
      "1600/1600 - 1s - loss: 0.0311 - accuracy: 0.9902 - val_loss: 0.1247 - val_accuracy: 0.9687\n"
     ]
    }
   ],
   "source": [
    "train_history = model.fit(train_images,train_labels_ohe,validation_split=0.2,epochs = train_epochs ,batch_size = batch_size,verbose = 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(60000, 28, 28)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_images.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.3607785e-02, -1.0932982e-02,  3.2487154e-02, ...,\n",
       "        -3.0765712e-02, -6.1155494e-02, -3.9775919e-02],\n",
       "       [-2.2081010e-02, -3.6634721e-02,  1.1371717e-02, ...,\n",
       "        -7.5042747e-02, -1.7965347e-02,  2.3989946e-02],\n",
       "       [-5.0838195e-02,  7.0013769e-02,  7.7197470e-02, ...,\n",
       "         7.3681511e-02,  1.9191399e-02,  2.1401696e-02],\n",
       "       ...,\n",
       "       [ 3.3442542e-02, -3.1780101e-02, -1.2028173e-02, ...,\n",
       "        -5.4749258e-02, -3.6396571e-02,  7.3060833e-02],\n",
       "       [ 8.2525916e-02,  1.0615617e-02, -6.6404469e-02, ...,\n",
       "         8.8438392e-06,  2.2789083e-02,  1.2731932e-02],\n",
       "       [ 6.9502257e-02, -3.5596326e-02,  6.9745928e-03, ...,\n",
       "         1.4537506e-02,  1.0804087e-02, -1.6509153e-02]], dtype=float32)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.trainable_variables[0].numpy()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
